package org.ruoyi.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.core.utils.StringUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.InterviewQuestionField;
import org.ruoyi.interview.domain.bo.InterviewQuestionFieldBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFieldVo;
import org.ruoyi.interview.mapper.InterviewQuestionFieldMapper;
import org.ruoyi.interview.service.IInterviewQuestionFieldService;

import java.util.Collection;
import java.util.List;

/**
 * 题目领域Service业务层实现
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class InterviewQuestionFieldServiceImpl implements IInterviewQuestionFieldService {

    private final InterviewQuestionFieldMapper baseMapper;

    /**
     * 查询题目领域
     *
     * @param fieldId 题目领域主键
     * @return 题目领域
     */
    @Override
    public InterviewQuestionFieldVo selectInterviewQuestionFieldById(Long fieldId) {
        return baseMapper.selectVoById(fieldId);
    }

    /**
     * 查询题目领域列表
     *
     * @param bo 题目领域业务对象
     * @param pageQuery 分页对象
     * @return 题目领域集合
     */
    @Override
    public TableDataInfo<InterviewQuestionFieldVo> selectInterviewQuestionFieldList(InterviewQuestionFieldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewQuestionField> lqw = buildQueryWrapper(bo);
        Page<InterviewQuestionFieldVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询题目领域列表
     *
     * @param bo 题目领域业务对象
     * @return 题目领域集合
     */
    @Override
    public List<InterviewQuestionFieldVo> selectInterviewQuestionFieldList(InterviewQuestionFieldBo bo) {
        LambdaQueryWrapper<InterviewQuestionField> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 新增题目领域
     *
     * @param bo 题目领域业务对象
     * @return 结果
     */
    @Override
    public Boolean insertInterviewQuestionField(InterviewQuestionFieldBo bo) {
        InterviewQuestionField add = MapstructUtils.convert(bo, InterviewQuestionField.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFieldId(add.getFieldId());
        }
        return flag;
    }

    /**
     * 修改题目领域
     *
     * @param bo 题目领域业务对象
     * @return 结果
     */
    @Override
    public Boolean updateInterviewQuestionField(InterviewQuestionFieldBo bo) {
        InterviewQuestionField update = MapstructUtils.convert(bo, InterviewQuestionField.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除题目领域
     *
     * @param fieldIds 需要删除的题目领域主键集合
     * @return 结果
     */
    @Override
    public Boolean deleteInterviewQuestionFieldByIds(Collection<Long> fieldIds) {
        return baseMapper.deleteBatchIds(fieldIds) > 0;
    }

    /**
     * 构建查询对象
     *
     * @param bo 题目领域业务对象
     * @return 查询对象
     */
    private LambdaQueryWrapper<InterviewQuestionField> buildQueryWrapper(InterviewQuestionFieldBo bo) {
        LambdaQueryWrapper<InterviewQuestionField> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFieldName()), InterviewQuestionField::getFieldName, bo.getFieldName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), InterviewQuestionField::getStatus, bo.getStatus());
        lqw.orderByAsc(InterviewQuestionField::getFieldId);
        return lqw;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InterviewQuestionField entity) {
        // TODO 根据业务逻辑添加校验
    }
}